﻿const express = require('express');
const router = express.Router();
const fs = require("fs");
const multer = require('multer');
const moment = require('moment');
const http = require('http');
const ScoreSegmentModel = require('../models/score-segment');
const ScoreSubItemModel = require('../models/score-sub-item');
const createGuid = require('../lib/util').createGuid;
const dateToString = require('../lib/util').dateToString;
const get_client_ip = require('../lib/util').get_client_ip;


const upload = multer({ dest: './public/cover' });
const upload2 = multer({ dest: './public/img' });


// 获取scoreModel
router.get('/:classGuid', function(req, res, next) {
    let classGuid = req.params.classGuid;
    ScoreSegmentModel.getListByClassGuid(classGuid, function(statusScoreSegmentList, valsScoreSegmentList){
        if (statusScoreSegmentList)
        {
            ScoreSubItemModel.getListOfClass(classGuid, function(statusScoreSubItemList, valsScoreSubItemList){
                if (statusScoreSubItemList)
                {
                    // 组成scoreSegment包含的scoreSubItemList
                    valsScoreSegmentList.forEach(elementScoreSegment => {
                        elementScoreSegment.subItemList = valsScoreSubItemList.filter(elementScoreSubItem => {
                            return elementScoreSegment.PK_ScoreSegmentGuid == elementScoreSubItem.FK_ScoreSegmentGuid;
                        });
                    });

                    res.send({ status: 1, data: valsScoreSegmentList });
                }
                else
                {
                    res.send({ status: 0, error: valsScoreSubItemList });
                }
            });
        }
        else
        {
            res.send({ status:0, error: valsScoreSegmentList });
        }
    });
});

router.post('/saveweight', function (req, res, next) {
    let ip = get_client_ip();
    ScoreSegmentModel.saveWeight(req.body.ScoreSegmentList, ip, function(statusSaveScoreSegmentWeight, valsSaveScoreSegmentWeight){
        if (statusSaveScoreSegmentWeight)
        {
            ScoreSubItemModel.saveWeight(req.body.SubItemList, ip, function(statusSaveScoreSubItemWeight, valsSavesScoreSubItemWeight){
                if (statusSaveScoreSubItemWeight)
                {
                    res.send({ status: 1, data: valsSavesScoreSubItemWeight });
                }
                else
                {
                    res.send({ status: 0, error: valsSavesScoreSubItemWeight });
                }
            });
        }
        else
        {
            res.send({ status: 0, error: valsSaveScoreSegmentWeight });
        }
    });
});

router.post('/initClassScoreModel/:id', function (req, res, next) {
    let classGuid = req.params.id;
    let ip = get_client_ip();
    ScoreSegmentModel.initClassScoreSegment(classGuid, ip, function(statusInitClassScoreSegment, valsInitClassScoreSegment) {
        if (statusInitClassScoreSegment)
        {
            ScoreSubItemModel.initClassScoreSubItem(classGuid, ip, function(statusInitClassScoreSubItem, valsInitClassScoreSubItem) {
                if (statusInitClassScoreSubItem)
                {
                    res.send({ status: 1, data: valsInitClassScoreSubItem });
                }
                else
                {
                    res.send({ status:0, error: valsInitClassScoreSubItem });
                }
            });
        }
        else
        {
            res.send({ status:0, error: valsInitClassScoreSegment });
        }
    });
});

module.exports = router;